unit uDZWinsock2;

interface

uses
  Windows, uDZQos;

const
  WINSOCK_VERSION = $0202;
  
type
  u_char = Byte;
  u_short = Word;
  u_int = Cardinal;
  u_long = Cardinal;
  u_int64 = Int64; 

  TSocket = Cardinal;
  
const
  FD_SETSIZE = 64;

type
  fd_set = record
    fd_count: u_int; 
    fd_array: array[0..FD_SETSIZE - 1] of TSocket;
  end;
  TFdSet = fd_set;
  PFdSet = ^fd_set;

function __WSAFDIsSet(s: TSocket; var FDSet: TFDSet): Integer; stdcall;
procedure FD_CLR(fd: TSocket; var fdset: TFdSet);
procedure _FD_SET(fd: TSocket; var fdset: TFDSet);
procedure FD_ZERO(var fdset: TFdSet);
function FD_ISSET(fd: TSocket; var fdset: TFdSet): Boolean;

type
  timeval = record
    tv_sec: Longint;
    tv_usec: Longint;
  end;

  TTimeVal = timeval;
  PTimeVal = ^timeval;

function timerisset(const tvp: TTimeVal): Boolean;
procedure timerclear(var tvp: TTimeVal);

const
  IOCPARM_MASK = $7F;
  IOC_VOID = $20000000;
  IOC_OUT = $40000000;
  IOC_IN = DWORD($80000000);
  IOC_INOUT = DWORD(IOC_IN or IOC_OUT);

function _IO(x, y: DWORD): DWORD;
function _IOR(x, y, t: DWORD): DWORD;
function _IOW(x, y, t: DWORD): DWORD;

const
  FIONREAD = IOC_OUT or ((SizeOf(u_long) and IOCPARM_MASK) shl 16) or (Ord('f')
    shl 8) or 127;
  FIONBIO = IOC_IN or ((SizeOf(u_long) and IOCPARM_MASK) shl 16) or (Ord('f') shl
    8) or 126;
  FIOASYNC = IOC_IN or ((SizeOf(u_long) and IOCPARM_MASK) shl 16) or (Ord('f')
    shl 8) or 125;
  SIOCSHIWAT = DWORD(IOC_IN or ((SizeOf(u_long) and IOCPARM_MASK) shl 16) or
    (Ord('s') shl 8) or 0);
  SIOCGHIWAT = IOC_OUT or ((SizeOf(u_long) and IOCPARM_MASK) shl 16) or (Ord('s')
    shl 8) or 1;
  SIOCSLOWAT = DWORD(IOC_IN or ((SizeOf(u_long) and IOCPARM_MASK) shl 16) or
    (Ord('s') shl 8) or 2);
  SIOCGLOWAT = IOC_OUT or ((SizeOf(u_long) and IOCPARM_MASK) shl 16) or (Ord('s')
    shl 8) or 3;
  SIOCATMARK = IOC_OUT or ((SizeOf(u_long) and IOCPARM_MASK) shl 16) or (Ord('s')
    shl 8) or 7;
    
type
  hostent = record
    h_name: PAnsiChar;
    h_aliases: PPAnsiChar;
    h_addrtype: Smallint;
    h_length: Smallint;
    case Integer of
      0: (h_addr_list: PPAnsiChar);
      1: (h_addr: PPAnsiChar); 
  end;
  THostEnt = hostent;
  PHostEnt = ^hostent;

type
  netent = record
    n_name: PAnsiChar;
    n_aliases: PPAnsiChar;
    n_addrtype: Smallint;
    n_net: u_long; 
  end;
  TNetEnt = netent;
  PNetEnt = ^netent;

  servent = record
    s_name: PAnsiChar;
    s_aliases: PPAnsiChar;
    s_port: Smallint;
    s_proto: PAnsiChar;
  end;

  TServEnt = servent;
  PServEnt = ^servent;

  protoent = record
    p_name: PAnsiChar;
    p_aliases: PPAnsiChar;
    p_proto: Smallint;
  end;
  TProtoEnt = protoent;
  PProtoEnt = ^protoent;

const
  IPPROTO_IP = 0;
  IPPROTO_HOPOPTS = 0;
  IPPROTO_ICMP = 1;
  IPPROTO_IGMP = 2;
  IPPROTO_GGP = 3;
  IPPROTO_IPV4 = 4;
  IPPROTO_TCP = 6;
  IPPROTO_PUP = 12;
  IPPROTO_UDP = 17;
  IPPROTO_IDP = 22;
  IPPROTO_IPV6 = 41;
  IPPROTO_ROUTING = 43;
  IPPROTO_FRAGMENT = 44;
  IPPROTO_ESP = 50;
  IPPROTO_AH = 51;
  IPPROTO_ICMPV6 = 58;
  IPPROTO_NONE = 59;
  IPPROTO_DSTOPTS = 60;
  IPPROTO_ND = 77;
  IPPROTO_ICLFXBM = 78;

  IPPROTO_RAW = 255;
  IPPROTO_MAX = 256;

  IPPORT_ECHO = 7;
  IPPORT_DISCARD = 9;
  IPPORT_SYSTAT = 11;
  IPPORT_DAYTIME = 13;
  IPPORT_NETSTAT = 15;
  IPPORT_FTP = 21;
  IPPORT_TELNET = 23;
  IPPORT_SMTP = 25;
  IPPORT_TIMESERVER = 37;
  IPPORT_NAMESERVER = 42;
  IPPORT_WHOIS = 43;
  IPPORT_MTP = 57;

  IPPORT_TFTP = 69;
  IPPORT_RJE = 77;
  IPPORT_FINGER = 79;
  IPPORT_TTYLINK = 87;
  IPPORT_SUPDUP = 95;

  IPPORT_EXECSERVER = 512;
  IPPORT_LOGINSERVER = 513;
  IPPORT_CMDSERVER = 514;
  IPPORT_EFSSERVER = 520;

  IPPORT_BIFFUDP = 512;
  IPPORT_WHOSERVER = 513;
  IPPORT_ROUTESERVER = 520;

  IPPORT_RESERVED = 1024;

  IMPLINK_IP = 155;
  IMPLINK_LOWEXPER = 156;
  IMPLINK_HIGHEXPER = 158;
  
type
  SunB = packed record
    s_b1, s_b2, s_b3, s_b4: u_char;
  end;

  SunC = packed record
    s_c1, s_c2, s_c3, s_c4: AnsiChar;
  end;

  SunW = packed record
    s_w1, s_w2: u_short;
  end;

  in_addr = record
    case Integer of
      0: (S_un_b: SunB);
      1: (S_un_c: SunC);
      2: (S_un_w: SunW);
      3: (S_addr: u_long);
  end;

  TInAddr = in_addr;
  PInAddr = ^in_addr;

function IN_CLASSA(i: DWORD): Boolean;

const
  IN_CLASSA_NET = DWORD($FF000000);
  IN_CLASSA_NSHIFT = 24;
  IN_CLASSA_HOST = $00FFFFFF;
  IN_CLASSA_MAX = 128;

function IN_CLASSB(i: DWORD): Boolean;

const
  IN_CLASSB_NET = DWORD($FFFF0000);
  IN_CLASSB_NSHIFT = 16;
  IN_CLASSB_HOST = $0000FFFF;
  IN_CLASSB_MAX = 65536;

function IN_CLASSC(i: DWORD): Boolean;

const
  IN_CLASSC_NET = DWORD($FFFFFF00);
  IN_CLASSC_NSHIFT = 8;
  IN_CLASSC_HOST = $000000FF;

function IN_CLASSD(i: DWORD): Boolean;

const
  IN_CLASSD_NET = DWORD($F0000000); 

  IN_CLASSD_NSHIFT = 28; 

  IN_CLASSD_HOST = $0FFFFFFF; 


function IN_MULTICAST(i: DWORD): Boolean;


const
  INADDR_ANY = u_long($00000000);

  INADDR_LOOPBACK = $7F000001;

  INADDR_BROADCAST = u_long($FFFFFFFF);

  INADDR_NONE = DWORD($FFFFFFFF);

  ADDR_ANY = INADDR_ANY;

type
  sockaddr_in = record
    sin_family: Smallint;
    sin_port: u_short;
    sin_addr: in_addr;
    sin_zero: array[0..7] of AnsiChar;
  end;

  TSockAddrIn = sockaddr_in;
  PSockAddrIn = ^sockaddr_in;

const
  WSADESCRIPTION_LEN = 256;

  WSASYS_STATUS_LEN = 128;


type
  WSAData = record
    wVersion: WORD;
    wHighVersion: WORD;
    szDescription: array[0..WSADESCRIPTION_LEN] of AnsiChar;
    szSystemStatus: array[0..WSASYS_STATUS_LEN] of AnsiChar;
    iMaxSockets: Word;
    iMaxUdpDg: Word;
    lpVendorInfo: PAnsiChar;
  end;

  LPWSADATA = ^WSAData;

  TWsaData = WSAData;
  PWsaData = LPWSADATA;

  //
  
  
  //

  //
  
  
  //

const
  INVALID_SOCKET = TSocket(not 0);

  SOCKET_ERROR = -1;


  //
  
  
  
  
  //

  FROM_PROTOCOL_INFO = -1;


  //
  
  //

  SOCK_STREAM = 1; 

  SOCK_DGRAM = 2; 

  SOCK_RAW = 3; 

  SOCK_RDM = 4; 

  SOCK_SEQPACKET = 5; 


  //
  
  //

  SO_DEBUG = $0001; 

  SO_ACCEPTCONN = $0002; 

  SO_REUSEADDR = $0004; 

  SO_KEEPALIVE = $0008; 

  SO_DONTROUTE = $0010; 

  SO_BROADCAST = $0020; 

  SO_USELOOPBACK = $0040; 

  SO_LINGER = $0080; 

  SO_OOBINLINE = $0100; 


  SO_DONTLINGER = DWORD(not SO_LINGER);

  SO_EXCLUSIVEADDRUSE = DWORD(not SO_REUSEADDR); 


  //
  
  //

  SO_SNDBUF = $1001; 

  SO_RCVBUF = $1002; 

  SO_SNDLOWAT = $1003; 

  SO_RCVLOWAT = $1004; 

  SO_SNDTIMEO = $1005; 

  SO_RCVTIMEO = $1006; 

  SO_ERROR = $1007; 

  SO_TYPE = $1008; 


  //
  
  //

  SO_GROUP_ID = $2001; 

  SO_GROUP_PRIORITY = $2002; 

  SO_MAX_MSG_SIZE = $2003; 

  SO_PROTOCOL_INFOA = $2004; 

  SO_PROTOCOL_INFOW = $2005; 


{$IFDEF UNICODE}
  SO_PROTOCOL_INFO = SO_PROTOCOL_INFOW;

{$ELSE}
  SO_PROTOCOL_INFO = SO_PROTOCOL_INFOA;

{$ENDIF UNICODE}

  PVD_CONFIG = $3001; 

  SO_CONDITIONAL_ACCEPT = $3002; 
  
  
  


  //
  
  //

  TCP_NODELAY = $0001;


  //
  
  //

  AF_UNSPEC = 0; 


  //
  
  
  
  
  
  
  
  //

  AF_UNIX = 1; 

  AF_INET = 2; 

  AF_IMPLINK = 3; 

  AF_PUP = 4; 

  AF_CHAOS = 5; 

  AF_NS = 6; 

  AF_IPX = AF_NS; 

  AF_ISO = 7; 

  AF_OSI = AF_ISO; 

  AF_ECMA = 8; 

  AF_DATAKIT = 9; 

  AF_CCITT = 10; 

  AF_SNA = 11; 

  AF_DECnet = 12; 

  AF_DLI = 13; 

  AF_LAT = 14; 

  AF_HYLINK = 15; 

  AF_APPLETALK = 16; 

  AF_NETBIOS = 17; 

  AF_VOICEVIEW = 18; 

  AF_FIREFOX = 19; 

  AF_UNKNOWN1 = 20; 

  AF_BAN = 21; 

  AF_ATM = 22; 

  AF_INET6 = 23; 

  AF_CLUSTER = 24; 

  AF_12844 = 25; 

  AF_IRDA = 26; 

  AF_NETDES = 28; 


  AF_TCNPROCESS = 29;

  AF_TCNMESSAGE = 30;

  AF_ICLFXBM = 31;


  AF_MAX = 32;


  //
  
  
  //

type
  sockaddr = record
    case Integer of
      0: (sin_family: u_short;
        sin_port: u_short;
        sin_zero: array[0..11] of AnsiChar);
      1: (sa_family: u_short;
        sa_data: array[0..13] of AnsiChar)
  end;

  TSockAddr = sockaddr;
  PSockAddr = ^sockaddr;

  //
  
  //

  //
  
  
  //

const
  _SS_MAXSIZE = 128; 

  _SS_ALIGNSIZE = SizeOf(Int64); 


  //
  
  //

  _SS_PAD1SIZE = _SS_ALIGNSIZE - SizeOf(short);

  _SS_PAD2SIZE = _SS_MAXSIZE - (SizeOf(short) + _SS_PAD1SIZE + _SS_ALIGNSIZE);


type
  sockaddr_storage = record
    ss_family: short; 
    __ss_pad1: array[0.._SS_PAD1SIZE - 1] of AnsiChar; 
    
    
    
    __ss_align: Int64; 
    __ss_pad2: array[0.._SS_PAD2SIZE - 1] of AnsiChar;
      
    
    
    
  end;

  TSockAddrStorage = sockaddr_storage;
  PSockAddrStorage = ^sockaddr_storage;

  sockproto = record
    sp_family: u_short; 
    sp_protocol: u_short; 
  end;

  TSockProto = sockproto;
  PSockProto = ^sockproto;
  
const
  PF_UNSPEC = AF_UNSPEC;

  PF_UNIX = AF_UNIX;

  PF_INET = AF_INET;

  PF_IMPLINK = AF_IMPLINK;

  PF_PUP = AF_PUP;

  PF_CHAOS = AF_CHAOS;

  PF_NS = AF_NS;

  PF_IPX = AF_IPX;

  PF_ISO = AF_ISO;

  PF_OSI = AF_OSI;

  PF_ECMA = AF_ECMA;

  PF_DATAKIT = AF_DATAKIT;

  PF_CCITT = AF_CCITT;

  PF_SNA = AF_SNA;

  PF_DECnet = AF_DECnet;

  PF_DLI = AF_DLI;

  PF_LAT = AF_LAT;

  PF_HYLINK = AF_HYLINK;

  PF_APPLETALK = AF_APPLETALK;

  PF_VOICEVIEW = AF_VOICEVIEW;

  PF_FIREFOX = AF_FIREFOX;

  PF_UNKNOWN1 = AF_UNKNOWN1;

  PF_BAN = AF_BAN;

  PF_ATM = AF_ATM;

  PF_INET6 = AF_INET6;


  PF_MAX = AF_MAX;


  //
  
  //

type
  linger = record
    l_onoff: u_short; 
    l_linger: u_short; 
  end;

  TLinger = linger;
  PLinger = ^linger;

  //
  
  //

const
  SOL_SOCKET = $FFFF; 


  //
  
  //

  SOMAXCONN = $7FFFFFFF;


  MSG_OOB = $1; 

  MSG_PEEK = $2; 

  MSG_DONTROUTE = $4;

  // do not complete until packet is completely 
  MSG_WAITALL = $8;

  MSG_PARTIAL = $8000; 


  MSG_INTERRUPT = $10; 


  MSG_MAXIOVLEN = 16;

  MAXGETHOSTSTRUCT = 1024;

  FD_READ_BIT = 0;

  FD_READ = 1 shl FD_READ_BIT;


  FD_WRITE_BIT = 1;

  FD_WRITE = 1 shl FD_WRITE_BIT;


  FD_OOB_BIT = 2;

  FD_OOB = 1 shl FD_OOB_BIT;


  FD_ACCEPT_BIT = 3;

  FD_ACCEPT = 1 shl FD_ACCEPT_BIT;


  FD_CONNECT_BIT = 4;

  FD_CONNECT = 1 shl FD_CONNECT_BIT;


  FD_CLOSE_BIT = 5;

  FD_CLOSE = 1 shl FD_CLOSE_BIT;


  FD_QOS_BIT = 6;

  FD_QOS = 1 shl FD_QOS_BIT;


  FD_GROUP_QOS_BIT = 7;

  FD_GROUP_QOS = 1 shl FD_GROUP_QOS_BIT;


  FD_ROUTING_INTERFACE_CHANGE_BIT = 8;

  FD_ROUTING_INTERFACE_CHANGE = 1 shl FD_ROUTING_INTERFACE_CHANGE_BIT;


  FD_ADDRESS_LIST_CHANGE_BIT = 9;

  FD_ADDRESS_LIST_CHANGE = 1 shl FD_ADDRESS_LIST_CHANGE_BIT;


  FD_MAX_EVENTS = 10;

  FD_ALL_EVENTS = (1 shl FD_MAX_EVENTS) - 1;


  //
  
  
  //
{$IFNDEF uDZ_INCLUDEMODE}
  WSABASEERR = 10000;


  //
  
  //

  WSAEINTR = WSABASEERR + 4;

  WSAEBADF = WSABASEERR + 9;

  WSAEACCES = WSABASEERR + 13;

  WSAEFAULT = WSABASEERR + 14;

  WSAEINVAL = WSABASEERR + 22;

  WSAEMFILE = WSABASEERR + 24;


  //
  
  //

  WSAEWOULDBLOCK = WSABASEERR + 35;

  WSAEINPROGRESS = WSABASEERR + 36;

  WSAEALREADY = WSABASEERR + 37;

  WSAENOTSOCK = WSABASEERR + 38;

  WSAEDESTADDRREQ = WSABASEERR + 39;

  WSAEMSGSIZE = WSABASEERR + 40;

  WSAEPROTOTYPE = WSABASEERR + 41;

  WSAENOPROTOOPT = WSABASEERR + 42;

  WSAEPROTONOSUPPORT = WSABASEERR + 43;

  WSAESOCKTNOSUPPORT = WSABASEERR + 44;

  WSAEOPNOTSUPP = WSABASEERR + 45;

  WSAEPFNOSUPPORT = WSABASEERR + 46;

  WSAEAFNOSUPPORT = WSABASEERR + 47;

  WSAEADDRINUSE = WSABASEERR + 48;

  WSAEADDRNOTAVAIL = WSABASEERR + 49;

  WSAENETDOWN = WSABASEERR + 50;

  WSAENETUNREACH = WSABASEERR + 51;

  WSAENETRESET = WSABASEERR + 52;

  WSAECONNABORTED = WSABASEERR + 53;

  WSAECONNRESET = WSABASEERR + 54;

  WSAENOBUFS = WSABASEERR + 55;

  WSAEISCONN = WSABASEERR + 56;

  WSAENOTCONN = WSABASEERR + 57;

  WSAESHUTDOWN = WSABASEERR + 58;

  WSAETOOMANYREFS = WSABASEERR + 59;

  WSAETIMEDOUT = WSABASEERR + 60;

  WSAECONNREFUSED = WSABASEERR + 61;

  WSAELOOP = WSABASEERR + 62;

  WSAENAMETOOLONG = WSABASEERR + 63;

  WSAEHOSTDOWN = WSABASEERR + 64;

  WSAEHOSTUNREACH = WSABASEERR + 65;

  WSAENOTEMPTY = WSABASEERR + 66;

  WSAEPROCLIM = WSABASEERR + 67;

  WSAEUSERS = WSABASEERR + 68;

  WSAEDQUOT = WSABASEERR + 69;

  WSAESTALE = WSABASEERR + 70;

  WSAEREMOTE = WSABASEERR + 71;


  //
  
  //

  WSASYSNOTREADY = WSABASEERR + 91;

  WSAVERNOTSUPPORTED = WSABASEERR + 92;

  WSANOTINITIALISED = WSABASEERR + 93;

  WSAEDISCON = WSABASEERR + 101;

  WSAENOMORE = WSABASEERR + 102;

  WSAECANCELLED = WSABASEERR + 103;

  WSAEINVALIDPROCTABLE = WSABASEERR + 104;

  WSAEINVALIDPROVIDER = WSABASEERR + 105;

  WSAEPROVIDERFAILEDINIT = WSABASEERR + 106;

  WSASYSCALLFAILURE = WSABASEERR + 107;

  WSASERVICE_NOT_FOUND = WSABASEERR + 108;

  WSATYPE_NOT_FOUND = WSABASEERR + 109;

  WSA_E_NO_MORE = WSABASEERR + 110;

  WSA_E_CANCELLED = WSABASEERR + 111;

  WSAEREFUSED = WSABASEERR + 112;


  //
  
  
  
  
  
  
  
  
  //

  

  WSAHOST_NOT_FOUND = WSABASEERR + 1001;


  

  WSATRY_AGAIN = WSABASEERR + 1002;


  

  WSANO_RECOVERY = WSABASEERR + 1003;


  

  WSANO_DATA = WSABASEERR + 1004;


  //
  
  //
  //

  WSA_QOS_RECEIVERS = WSABASEERR + 1005; 

  WSA_QOS_SENDERS = WSABASEERR + 1006; 

  WSA_QOS_NO_SENDERS = WSABASEERR + 1007; 

  WSA_QOS_NO_RECEIVERS = WSABASEERR + 1008; 

  WSA_QOS_REQUEST_CONFIRMED = WSABASEERR + 1009; 

  WSA_QOS_ADMISSION_FAILURE = WSABASEERR + 1010;
    

  WSA_QOS_POLICY_FAILURE = WSABASEERR + 1011;
    

  WSA_QOS_BAD_STYLE = WSABASEERR + 1012; 

  WSA_QOS_BAD_OBJECT = WSABASEERR + 1013;
    

  WSA_QOS_TRAFFIC_CTRL_ERROR = WSABASEERR + 1014;
    

  WSA_QOS_GENERIC_ERROR = WSABASEERR + 1015; 

  WSA_QOS_ESERVICETYPE = WSABASEERR + 1016; 

  WSA_QOS_EFLOWSPEC = WSABASEERR + 1017; 

  WSA_QOS_EPROVSPECBUF = WSABASEERR + 1018; 

  WSA_QOS_EFILTERSTYLE = WSABASEERR + 1019; 

  WSA_QOS_EFILTERTYPE = WSABASEERR + 1020; 

  WSA_QOS_EFILTERCOUNT = WSABASEERR + 1021; 

  WSA_QOS_EOBJLENGTH = WSABASEERR + 1022; 

  WSA_QOS_EFLOWCOUNT = WSABASEERR + 1023; 

  WSA_QOS_EUNKOWNPSOBJ = WSABASEERR + 1024;
    

  WSA_QOS_EPOLICYOBJ = WSABASEERR + 1025;
    

  WSA_QOS_EFLOWDESC = WSABASEERR + 1026; 

  WSA_QOS_EPSFLOWSPEC = WSABASEERR + 1027;
    

  WSA_QOS_EPSFILTERSPEC = WSABASEERR + 1028;
    

  WSA_QOS_ESDMODEOBJ = WSABASEERR + 1029;
    

  WSA_QOS_ESHAPERATEOBJ = WSABASEERR + 1030;
    

  WSA_QOS_RESERVED_PETYPE = WSABASEERR + 1031;
    

{$ENDIF uDZ_INCLUDEMODE}

  //
  
  //

function h_errno: Integer;


const
  HOST_NOT_FOUND = WSAHOST_NOT_FOUND;

  TRY_AGAIN = WSATRY_AGAIN;

  NO_RECOVERY = WSANO_RECOVERY;

  NO_DATA = WSANO_DATA;


  

  WSANO_ADDRESS = WSANO_DATA;

  NO_ADDRESS = WSANO_ADDRESS;


  //
  
  
  
  //

{$IFDEF FALSE}

const
  EWOULDBLOCK = WSAEWOULDBLOCK;

  EINPROGRESS = WSAEINPROGRESS;

  EALREADY = WSAEALREADY;

  ENOTSOCK = WSAENOTSOCK;

  EDESTADDRREQ = WSAEDESTADDRREQ;

  EMSGSIZE = WSAEMSGSIZE;

  EPROTOTYPE = WSAEPROTOTYPE;

  ENOPROTOOPT = WSAENOPROTOOPT;

  EPROTONOSUPPORT = WSAEPROTONOSUPPORT;

  ESOCKTNOSUPPORT = WSAESOCKTNOSUPPORT;

  EOPNOTSUPP = WSAEOPNOTSUPP;

  EPFNOSUPPORT = WSAEPFNOSUPPORT;

  EAFNOSUPPORT = WSAEAFNOSUPPORT;

  EADDRINUSE = WSAEADDRINUSE;

  EADDRNOTAVAIL = WSAEADDRNOTAVAIL;

  ENETDOWN = WSAENETDOWN;

  ENETUNREACH = WSAENETUNREACH;

  ENETRESET = WSAENETRESET;

  ECONNABORTED = WSAECONNABORTED;

  ECONNRESET = WSAECONNRESET;

  ENOBUFS = WSAENOBUFS;

  EISCONN = WSAEISCONN;

  ENOTCONN = WSAENOTCONN;

  ESHUTDOWN = WSAESHUTDOWN;

  ETOOMANYREFS = WSAETOOMANYREFS;

  ETIMEDOUT = WSAETIMEDOUT;

  ECONNREFUSED = WSAECONNREFUSED;

  ELOOP = WSAELOOP;

  ENAMETOOLONG = WSAENAMETOOLONG;

  EHOSTDOWN = WSAEHOSTDOWN;

  EHOSTUNREACH = WSAEHOSTUNREACH;

  ENOTEMPTY = WSAENOTEMPTY;

  EPROCLIM = WSAEPROCLIM;

  EUSERS = WSAEUSERS;

  EDQUOT = WSAEDQUOT;

  ESTALE = WSAESTALE;

  EREMOTE = WSAEREMOTE;


{$ENDIF FALSE}

  //
  
  //

type
  WSAEVENT = THANDLE;

  LPWSAEVENT = PHANDLE;

  WSAOVERLAPPED = OVERLAPPED;

  LPWSAOVERLAPPED = POVERLAPPED;

  TWsaEvent = WSAEVENT;
  PWsaEvent = LPWSAEVENT;
  TWsaOverlapped = WSAOVERLAPPED;
  PWsaOverlapped = LPWSAOVERLAPPED;

const
  WSA_IO_PENDING = ERROR_IO_PENDING;

  WSA_IO_INCOMPLETE = ERROR_IO_INCOMPLETE;

  WSA_INVALID_HANDLE = ERROR_INVALID_HANDLE;

  WSA_INVALID_PARAMETER = ERROR_INVALID_PARAMETER;

  WSA_NOT_ENOUGH_MEMORY = ERROR_NOT_ENOUGH_MEMORY;

  WSA_OPERATION_ABORTED = ERROR_OPERATION_ABORTED;


  WSA_INVALID_EVENT = WSAEVENT(nil);

  WSA_MAXIMUM_WAIT_EVENTS = MAXIMUM_WAIT_OBJECTS;

  WSA_WAIT_FAILED = WAIT_FAILED;

  WSA_WAIT_EVENT_0 = WAIT_OBJECT_0;

  WSA_WAIT_IO_COMPLETION = WAIT_IO_COMPLETION;

  WSA_WAIT_TIMEOUT = WAIT_TIMEOUT;

  WSA_INFINITE = INFINITE;


  //
  
  
  //

type
  _WSABUF = record
    len: u_long; 
    buf: PAnsiChar; 
  end;

  WSABUF = _WSABUF;

  LPWSABUF = ^_WSABUF;

  TWsaBuf = WSABUF;
  PWsaBuf = LPWSABUF;

  _QualityOfService = record
    SendingFlowspec: FLOWSPEC; 
    ReceivingFlowspec: FLOWSPEC; 
    ProviderSpecific: WSABUF; 
  end;

  QOS = _QualityOfService;

  LPQOS = ^_QualityOfService;

  TQOS = QOS;
  PQOS = LPQOS;

  //
  
  //

const
  CF_ACCEPT = $0000;

  CF_REJECT = $0001;

  CF_DEFER = $0002;


  //
  
  //

  SD_RECEIVE = $00;

  SD_SEND = $01;

  SD_BOTH = $02;


  //
  
  //

type
  GROUP = Cardinal;

  TGroup = GROUP;
  PGroup = ^GROUP;

const
  SG_UNCONSTRAINED_GROUP = $01;

  SG_CONSTRAINED_GROUP = $02;


  //
  
  //

type
  _WSANETWORKEVENTS = record
    lNetworkEvents: Longint;
    iErrorCode: array[0..FD_MAX_EVENTS - 1] of Integer;
  end;

  WSANETWORKEVENTS = _WSANETWORKEVENTS;

  LPWSANETWORKEVENTS = ^WSANETWORKEVENTS;

  TWsaNetworkEvents = WSANETWORKEVENTS;
  PWsaNetworkEvents = LPWSANETWORKEVENTS;

  //
  
  
  //

const
  MAX_PROTOCOL_CHAIN = 7;


  BASE_PROTOCOL = 1;

  LAYERED_PROTOCOL = 0;


type
  _WSAPROTOCOLCHAIN = record
    ChainLen: Integer; 
    
    
    
    ChainEntries: array[0..MAX_PROTOCOL_CHAIN - 1] of DWORD;
      
  end;

  WSAPROTOCOLCHAIN = _WSAPROTOCOLCHAIN;

  LPWSAPROTOCOLCHAIN = ^WSAPROTOCOLCHAIN;

  TWsaProtocolChain = WSAPROTOCOLCHAIN;
  PWsaProtocolChain = LPWSAPROTOCOLCHAIN;

const
  WSAPROTOCOL_LEN = 255;


type
  _WSAPROTOCOL_INFOA = record
    dwServiceFlags1: DWORD;
    dwServiceFlags2: DWORD;
    dwServiceFlags3: DWORD;
    dwServiceFlags4: DWORD;
    dwProviderFlags: DWORD;
    ProviderId: TGUID;
    dwCatalogEntryId: DWORD;
    ProtocolChain: WSAPROTOCOLCHAIN;
    iVersion: Integer;
    iAddressFamily: Integer;
    iMaxSockAddr: Integer;
    iMinSockAddr: Integer;
    iSocketType: Integer;
    iProtocol: Integer;
    iProtocolMaxOffset: Integer;
    iNetworkByteOrder: Integer;
    iSecurityScheme: Integer;
    dwMessageSize: DWORD;
    dwProviderReserved: DWORD;
    szProtocol: array[0..WSAPROTOCOL_LEN] of AnsiChar;
  end;

  WSAPROTOCOL_INFOA = _WSAPROTOCOL_INFOA;
  LPWSAPROTOCOL_INFOA = ^WSAPROTOCOL_INFOA;

  TWsaProtocolInfoA = WSAPROTOCOL_INFOA;
  PWsaProtocolInfoA = LPWSAPROTOCOL_INFOA;

  _WSAPROTOCOL_INFOW = record
    dwServiceFlags1: DWORD;
    dwServiceFlags2: DWORD;
    dwServiceFlags3: DWORD;
    dwServiceFlags4: DWORD;
    dwProviderFlags: DWORD;
    ProviderId: TGUID;
    dwCatalogEntryId: DWORD;
    ProtocolChain: WSAPROTOCOLCHAIN;
    iVersion: Integer;
    iAddressFamily: Integer;
    iMaxSockAddr: Integer;
    iMinSockAddr: Integer;
    iSocketType: Integer;
    iProtocol: Integer;
    iProtocolMaxOffset: Integer;
    iNetworkByteOrder: Integer;
    iSecurityScheme: Integer;
    dwMessageSize: DWORD;
    dwProviderReserved: DWORD;
    szProtocol: array[0..WSAPROTOCOL_LEN] of WideChar;
  end;

  WSAPROTOCOL_INFOW = _WSAPROTOCOL_INFOW;

  LPWSAPROTOCOL_INFOW = ^WSAPROTOCOL_INFOW;

  TWsaProtocolInfoW = WSAPROTOCOL_INFOW;
  PWsaProtocolInfoW = LPWSAPROTOCOL_INFOW;

{$IFDEF UNICODE}
  WSAPROTOCOL_INFO = WSAPROTOCOL_INFOW;

  LPWSAPROTOCOL_INFO = LPWSAPROTOCOL_INFOW;

  TWsaProtocolInfo = TWsaProtocolInfoW;
  PWsaProtocolInfo = PWsaProtocolInfoW;
{$ELSE}
  WSAPROTOCOL_INFO = WSAPROTOCOL_INFOA;

  LPWSAPROTOCOL_INFO = LPWSAPROTOCOL_INFOA;

  TWsaProtocolInfo = TWsaProtocolInfoA;
  PWsaProtocolInfo = PWsaProtocolInfoA;
{$ENDIF UNICODE}

  

const
  PFL_MULTIPLE_PROTO_ENTRIES = $00000001;

  PFL_RECOMMENDED_PROTO_ENTRY = $00000002;

  PFL_HIDDEN = $00000004;

  PFL_MATCHES_PROTOCOL_ZERO = $00000008;


  

  XP1_CONNECTIONLESS = $00000001;

  XP1_GUARANTEED_DELIVERY = $00000002;

  XP1_GUARANTEED_ORDER = $00000004;

  XP1_MESSAGE_ORIENTED = $00000008;

  XP1_PSEUDO_STREAM = $00000010;

  XP1_GRACEFUL_CLOSE = $00000020;

  XP1_EXPEDITED_DATA = $00000040;

  XP1_CONNECT_DATA = $00000080;

  XP1_DISCONNECT_DATA = $00000100;

  XP1_SUPPORT_BROADCAST = $00000200;

  XP1_SUPPORT_MULTIPOINT = $00000400;

  XP1_MULTIPOINT_CONTROL_PLANE = $00000800;

  XP1_MULTIPOINT_DATA_PLANE = $00001000;

  XP1_QOS_SUPPORTED = $00002000;

  XP1_INTERRUPT = $00004000;

  XP1_UNI_SEND = $00008000;

  XP1_UNI_RECV = $00010000;

  XP1_IFS_HANDLES = $00020000;

  XP1_PARTIAL_MESSAGE = $00040000;


  BIGENDIAN = $0000;

  LITTLEENDIAN = $0001;


  SECURITY_PROTOCOL_NONE = $0000;
  JL_SENDER_ONLY = $01;
  JL_RECEIVER_ONLY = $02;
  JL_BOTH = $04;
  
  WSA_FLAG_OVERLAPPED = $01;
  WSA_FLAG_MULTIPOINT_C_ROOT = $02;
  WSA_FLAG_MULTIPOINT_C_LEAF = $04;
  WSA_FLAG_MULTIPOINT_D_ROOT = $08;
  WSA_FLAG_MULTIPOINT_D_LEAF = $10;

  IOC_UNIX = $00000000;
  IOC_WS2 = $08000000;
  IOC_PROTOCOL = $10000000;
  IOC_VENDOR = $18000000;

function _WSAIO(x, y: DWORD): DWORD;
function _WSAIOR(x, y: DWORD): DWORD;
function _WSAIOW(x, y: DWORD): DWORD;
function _WSAIORW(x, y: DWORD): DWORD;


const
  SIO_ASSOCIATE_HANDLE = IOC_INOUT or IOC_WS2 or 1;
  SIO_ENABLE_CIRCULAR_QUEUEING = IOC_VOID or IOC_WS2 or 2;
  SIO_FIND_ROUTE = IOC_OUT or IOC_WS2 or 3;
  SIO_FLUSH = IOC_VOID or IOC_WS2 or 4;
  SIO_GET_BROADCAST_ADDRESS = IOC_OUT or IOC_WS2 or 5;
  SIO_GET_EXTENSION_FUNCTION_POINTER = IOC_INOUT or IOC_WS2 or 6;
  SIO_GET_QOS = IOC_INOUT or IOC_WS2 or 7;
  SIO_GET_GROUP_QOS = IOC_INOUT or IOC_WS2 or 8;
  SIO_MULTIPOINT_LOOPBACK = IOC_INOUT or IOC_WS2 or 9;
  SIO_MULTICAST_SCOPE = IOC_INOUT or IOC_WS2 or 10;
  SIO_SET_QOS = IOC_INOUT or IOC_WS2 or 11;
  SIO_SET_GROUP_QOS = IOC_INOUT or IOC_WS2 or 12;
  SIO_TRANSLATE_HANDLE = IOC_INOUT or IOC_WS2 or 13;
  SIO_ROUTING_INTERFACE_QUERY = IOC_INOUT or IOC_WS2 or 20;
  SIO_ROUTING_INTERFACE_CHANGE = IOC_INOUT or IOC_WS2 or 21;
  SIO_ADDRESS_LIST_QUERY = IOC_OUT or IOC_WS2 or 22;
  SIO_ADDRESS_LIST_CHANGE = IOC_VOID or IOC_WS2 or 23;
  SIO_QUERY_TARGET_PNP_HANDLE = IOC_OUT or IOC_WS2 or 24;
  SIO_ADDRESS_LIST_SORT = IOC_INOUT or IOC_WS2 or 25;

type
  LPCONDITIONPROC = function(lpCallerId, lpCallerData: LPWSABUF; lpSQOS, lpGQOS:
    LPQOS; lpCalleeId, lpCalleeData: LPWSABUF;
    g: PGROUP; dwCallbackData: LongWord): Integer; stdcall;
  LPWSAOVERLAPPED_COMPLETION_ROUTINE = procedure(dwError, cbTransferred: DWORD;
    lpOverlapped: LPWSAOVERLAPPED; dwFlags: DWORD); stdcall;

const
  SIO_NSP_NOTIFY_CHANGE = IOC_IN or IOC_WS2 or 25;

type
  _WSACOMPLETIONTYPE = (
    NSP_NOTIFY_IMMEDIATELY,
    NSP_NOTIFY_HWND,
    NSP_NOTIFY_EVENT,
    NSP_NOTIFY_PORT,
    NSP_NOTIFY_APC);

  WSACOMPLETIONTYPE = _WSACOMPLETIONTYPE;
  PWSACOMPLETIONTYPE = ^WSACOMPLETIONTYPE;
  LPWSACOMPLETIONTYPE = ^WSACOMPLETIONTYPE;
  TWsaCompletionType = WSACOMPLETIONTYPE;

  _WSACOMPLETION = record
    Type_: WSACOMPLETIONTYPE;
    case Integer of 
      0: (
        hWnd: HWND;
        uMsg: UINT;
        context: WPARAM);
      (* TODO
      1: ( 
        lpOverlapped: LPWSAOVERLAPPED);
      2: ( 
        lpOverlapped: LPWSAOVERLAPPED;
        lpfnCompletionProc: LPWSAOVERLAPPED_COMPLETION_ROUTINE);
      *)
      3: (
        lpOverlapped: LPWSAOVERLAPPED;
        hPort: THANDLE;
        Key: LongWord);
  end;

  WSACOMPLETION = _WSACOMPLETION;
  PWSACOMPLETION = ^WSACOMPLETION;
  LPWSACOMPLETION = ^WSACOMPLETION;
  TWsaCompletion = WSACOMPLETION;

const
  TH_NETDEV = $00000001;
  TH_TAPI = $00000002;

type
  LPSOCKADDR = ^sockaddr;
  PSOCKADDR_STORAGE = ^sockaddr_storage;
  LPSOCKADDR_STORAGE = ^sockaddr_storage;

  _BLOB = record
    cbSize: ULONG;
    pBlobData: PBYTE;
  end;

  BLOB = _BLOB;
  LPBLOB = ^BLOB;
  TBlob = BLOB;
  PBlob = LPBLOB;

const
  SERVICE_MULTIPLE = $00000001;

  NS_ALL = 0;
  NS_SAP = 1;
  NS_NDS = 2;
  NS_PEER_BROWSE = 3;
  NS_SLP = 5;
  NS_DHCP = 6;
  NS_TCPIP_LOCAL = 10;
  NS_TCPIP_HOSTS = 11;
  NS_DNS = 12;
  NS_NETBT = 13;
  NS_WINS = 14;
  NS_NLA = 15; 
  NS_NBP = 20;
  NS_MS = 30;
  NS_STDA = 31;
  NS_NTDS = 32;
  NS_X500 = 40;
  NS_NIS = 41;
  NS_NISPLUS = 42;
  NS_WRQ = 50;
  NS_NETDES = 60; 

  RES_UNUSED_1 = $00000001;
  RES_FLUSH_CACHE = $00000002;
  RES_SERVICE = $00000004;

  SERVICE_TYPE_VALUE_IPXPORTA = 'IpxSocket';
  SERVICE_TYPE_VALUE_IPXPORTW = WideString('IpxSocket');
  SERVICE_TYPE_VALUE_SAPIDA = 'SapId';
  SERVICE_TYPE_VALUE_SAPIDW = WideString('SapId');
  SERVICE_TYPE_VALUE_TCPPORTA = 'TcpPort';
  SERVICE_TYPE_VALUE_TCPPORTW = WideString('TcpPort');
  SERVICE_TYPE_VALUE_UDPPORTA = 'UdpPort';
  SERVICE_TYPE_VALUE_UDPPORTW = WideString('UdpPort');
  SERVICE_TYPE_VALUE_OBJECTIDA = 'ObjectId';
  SERVICE_TYPE_VALUE_OBJECTIDW = WideString('ObjectId');

{$IFDEF UNICODE}
  SERVICE_TYPE_VALUE_SAPID = SERVICE_TYPE_VALUE_SAPIDW;
  SERVICE_TYPE_VALUE_TCPPORT = SERVICE_TYPE_VALUE_TCPPORTW;
  SERVICE_TYPE_VALUE_UDPPORT = SERVICE_TYPE_VALUE_UDPPORTW;
  SERVICE_TYPE_VALUE_OBJECTID = SERVICE_TYPE_VALUE_OBJECTIDW;
{$ELSE}
  SERVICE_TYPE_VALUE_SAPID = SERVICE_TYPE_VALUE_SAPIDA;
  SERVICE_TYPE_VALUE_TCPPORT = SERVICE_TYPE_VALUE_TCPPORTA;
  SERVICE_TYPE_VALUE_UDPPORT = SERVICE_TYPE_VALUE_UDPPORTA;
  SERVICE_TYPE_VALUE_OBJECTID = SERVICE_TYPE_VALUE_OBJECTIDA;
{$ENDIF UNICODE}

type
  LPSOCKET_ADDRESS = ^SOCKET_ADDRESS;
  PSOCKET_ADDRESS = ^SOCKET_ADDRESS;
  _SOCKET_ADDRESS = record
    lpSockaddr: LPSOCKADDR;
    iSockaddrLength: Integer;
  end;
  SOCKET_ADDRESS = _SOCKET_ADDRESS;
  TSocketAddress = SOCKET_ADDRESS;
  PSocketAddress = PSOCKET_ADDRESS;

  LPCSADDR_INFO = ^CSADDR_INFO;
  PCSADDR_INFO = ^CSADDR_INFO;
  _CSADDR_INFO = record
    LocalAddr: SOCKET_ADDRESS;
    RemoteAddr: SOCKET_ADDRESS;
    iSocketType: Integer;
    iProtocol: Integer;
  end;

  CSADDR_INFO = _CSADDR_INFO;
  TCsAddrInfo = CSADDR_INFO;
  PCsAddrInfo = PCSADDR_INFO;

  LPSOCKET_ADDRESS_LIST = ^SOCKET_ADDRESS_LIST;
  _SOCKET_ADDRESS_LIST = record
    iAddressCount: Integer;
    Address: array[0..0] of SOCKET_ADDRESS;
  end;
  SOCKET_ADDRESS_LIST = _SOCKET_ADDRESS_LIST;
  TSocketAddressList = SOCKET_ADDRESS_LIST;
  PSocketAddressList = LPSOCKET_ADDRESS_LIST;
  
  LPAFPROTOCOLS = ^AFPROTOCOLS;
  PAFPROTOCOLS = ^AFPROTOCOLS;
  _AFPROTOCOLS = record
    iAddressFamily: Integer;
    iProtocol: Integer;
  end;
  AFPROTOCOLS = _AFPROTOCOLS;
  TAfProtocols = AFPROTOCOLS;

  _WSAEcomparator = (COMP_EQUAL, COMP_NOTLESS);
  WSAECOMPARATOR = _WSAEcomparator;
  PWSAECOMPARATOR = ^WSAECOMPARATOR;
  LPWSAECOMPARATOR = ^WSAECOMPARATOR;
  
  LPWSAVERSION = ^WSAVERSION;
  PWSAVERSION = ^WSAVERSION;
  _WSAVersion = record
    dwVersion: DWORD;
    ecHow: WSAECOMPARATOR;
  end;
  WSAVERSION = _WSAVersion;
  TWsaVersion = WSAVERSION;
  
  _WSAQuerySetA = record
    dwSize: DWORD;
    lpszServiceInstanceName: LPSTR;
    lpServiceClassId: PGUID;
    lpVersion: LPWSAVERSION;
    lpszComment: LPSTR;
    dwNameSpace: DWORD;
    lpNSProviderId: PGUID;
    lpszContext: LPSTR;
    dwNumberOfProtocols: DWORD;
    lpafpProtocols: LPAFPROTOCOLS;
    lpszQueryString: LPSTR;
    dwNumberOfCsAddrs: DWORD;
    lpcsaBuffer: LPCSADDR_INFO;
    dwOutputFlags: DWORD;
    lpBlob: LPBLOB;
  end;
  WSAQUERYSETA = _WSAQuerySetA;
  PWSAQUERYSETA = ^WSAQUERYSETA;
  LPWSAQUERYSETA = ^WSAQUERYSETA;
  TWsaQuerySetA = WSAQUERYSETA;
  
  _WSAQuerySetW = record
    dwSize: DWORD;
    lpszServiceInstanceName: LPWSTR;
    lpServiceClassId: PGUID;
    lpVersion: LPWSAVERSION;
    lpszComment: LPWSTR;
    dwNameSpace: DWORD;
    lpNSProviderId: PGUID;
    lpszContext: LPWSTR;
    dwNumberOfProtocols: DWORD;
    lpafpProtocols: LPAFPROTOCOLS;
    lpszQueryString: LPWSTR;
    dwNumberOfCsAddrs: DWORD;
    lpcsaBuffer: LPCSADDR_INFO;
    dwOutputFlags: DWORD;
    lpBlob: LPBLOB;
  end;
  WSAQUERYSETW = _WSAQuerySetW;
  PWSAQUERYSETW = ^WSAQUERYSETW;
  LPWSAQUERYSETW = ^WSAQUERYSETW;
  TWsaQuerySetW = WSAQUERYSETW;

{$IFDEF UNICODE}
  WSAQUERYSET = WSAQUERYSETW;
  PWSAQUERYSET = PWSAQUERYSETW;
  LPWSAQUERYSET = LPWSAQUERYSETW;
  TWsaQuerySet = TWsaQuerySetW;
{$ELSE}
  WSAQUERYSET = WSAQUERYSETA;
  PWSAQUERYSET = PWSAQUERYSETA;
  LPWSAQUERYSET = LPWSAQUERYSETA;
  TWsaQuerySet = TWsaQuerySetA;
{$ENDIF UNICODE}

const
  LUP_DEEP = $0001;
  LUP_CONTAINERS = $0002;
  LUP_NOCONTAINERS = $0004;
  LUP_NEAREST = $0008;
  LUP_RETURN_NAME = $0010;
  LUP_RETURN_TYPE = $0020;
  LUP_RETURN_VERSION = $0040;
  LUP_RETURN_COMMENT = $0080;
  LUP_RETURN_ADDR = $0100;
  LUP_RETURN_BLOB = $0200;
  LUP_RETURN_ALIASES = $0400;
  LUP_RETURN_QUERY_STRING = $0800;
  LUP_RETURN_ALL = $0FF0;
  LUP_RES_SERVICE = $8000;
  LUP_FLUSHCACHE = $1000;
  LUP_FLUSHPREVIOUS = $2000;

  RESULT_IS_ALIAS = $0001;
  RESULT_IS_ADDED = $0010;
  RESULT_IS_CHANGED = $0020;
  RESULT_IS_DELETED = $0040;

type
  _WSAESETSERVICEOP = (RNRSERVICE_REGISTER, RNRSERVICE_DEREGISTER,
    RNRSERVICE_DELETE);
  WSAESETSERVICEOP = _WSAESETSERVICEOP;
  PWSAESETSERVICEOP = ^WSAESETSERVICEOP;
  LPWSAESETSERVICEOP = ^WSAESETSERVICEOP;

  _WSANSClassInfoA = record
    lpszName: LPSTR;
    dwNameSpace: DWORD;
    dwValueType: DWORD;
    dwValueSize: DWORD;
    lpValue: Pointer;
  end;

  WSANSCLASSINFOA = _WSANSClassInfoA;
  PWSANSCLASSINFOA = ^WSANSCLASSINFOA;
  LPWSANSCLASSINFOA = ^WSANSCLASSINFOA;
  TWsaNsClassInfoA = WSANSCLASSINFOA;
  _WSANSClassInfoW = record
    lpszName: LPWSTR;
    dwNameSpace: DWORD;
    dwValueType: DWORD;
    dwValueSize: DWORD;
    lpValue: Pointer;
  end;

  WSANSCLASSINFOW = _WSANSClassInfoW;
  PWSANSCLASSINFOW = ^WSANSCLASSINFOW;
  LPWSANSCLASSINFOW = ^WSANSCLASSINFOW;
  TWsaNsClassInfoW = WSANSCLASSINFOW;

{$IFDEF UNICODE}
  WSANSCLASSINFO = WSANSCLASSINFOW;
  PWSANSCLASSINFO = PWSANSCLASSINFOW;
  LPWSANSCLASSINFO = LPWSANSCLASSINFOW;
  TWsaNsClassInfo = TWsaNsClassInfoW;
{$ELSE}
  WSANSCLASSINFO = WSANSCLASSINFOA;
  PWSANSCLASSINFO = PWSANSCLASSINFOA;
  LPWSANSCLASSINFO = LPWSANSCLASSINFOA;
  TWsaNsClassInfo = TWsaNsClassInfoA;
{$ENDIF UNICODE}

  _WSAServiceClassInfoA = record
    lpServiceClassId: PGUID;
    lpszServiceClassName: LPSTR;
    dwCount: DWORD;
    lpClassInfos: LPWSANSCLASSINFOA;
  end;
  WSASERVICECLASSINFOA = _WSAServiceClassInfoA;
  PWSASERVICECLASSINFOA = ^WSASERVICECLASSINFOA;
  LPWSASERVICECLASSINFOA = ^WSASERVICECLASSINFOA;
  TWsaServiceClassInfoA = WSASERVICECLASSINFOA;

  _WSAServiceClassInfoW = record
    lpServiceClassId: PGUID;
    lpszServiceClassName: LPWSTR;
    dwCount: DWORD;
    lpClassInfos: LPWSANSCLASSINFOW;
  end;
  WSASERVICECLASSINFOW = _WSAServiceClassInfoW;
  PWSASERVICECLASSINFOW = ^WSASERVICECLASSINFOW;
  LPWSASERVICECLASSINFOW = ^WSASERVICECLASSINFOW;
  TWsaServiceClassInfoW = WSASERVICECLASSINFOW;

{$IFDEF UNICODE}
  WSASERVICECLASSINFO = WSASERVICECLASSINFOW;
  PWSASERVICECLASSINFO = PWSASERVICECLASSINFOW;
  LPWSASERVICECLASSINFO = LPWSASERVICECLASSINFOW;
  TWsaServiceClassInfo = TWsaServiceClassInfoW;
{$ELSE}
  WSASERVICECLASSINFO = WSASERVICECLASSINFOA;
  PWSASERVICECLASSINFO = PWSASERVICECLASSINFOA;
  LPWSASERVICECLASSINFO = LPWSASERVICECLASSINFOA;
  TWsaServiceClassInfo = TWsaServiceClassInfoA;
{$ENDIF UNICODE}

  LPWSANAMESPACE_INFOA = ^WSANAMESPACE_INFOA;
  PWSANAMESPACE_INFOA = ^WSANAMESPACE_INFOA;
  _WSANAMESPACE_INFOA = record
    NSProviderId: TGUID;
    dwNameSpace: DWORD;
    fActive: BOOL;
    dwVersion: DWORD;
    lpszIdentifier: LPSTR;
  end;
  WSANAMESPACE_INFOA = _WSANAMESPACE_INFOA;
  TWsaNameSpaceInfoA = WSANAMESPACE_INFOA;
  PWsaNameSpaceInfoA = LPWSANAMESPACE_INFOA;

  LPWSANAMESPACE_INFOW = ^WSANAMESPACE_INFOW;
  PWSANAMESPACE_INFOW = ^WSANAMESPACE_INFOW;
  _WSANAMESPACE_INFOW = record
    NSProviderId: TGUID;
    dwNameSpace: DWORD;
    fActive: BOOL;
    dwVersion: DWORD;
    lpszIdentifier: LPWSTR;
  end;
  WSANAMESPACE_INFOW = _WSANAMESPACE_INFOW;
  TWsaNameSpaceInfoW = WSANAMESPACE_INFOW;
  PWsaNameSpaceInfoW = PWSANAMESPACE_INFOW;

{$IFDEF UNICODE}
  WSANAMESPACE_INFO = WSANAMESPACE_INFOW;
  PWSANAMESPACE_INFO = PWSANAMESPACE_INFOW;
  LPWSANAMESPACE_INFO = LPWSANAMESPACE_INFOW;
  TWsaNameSpaceInfo = TWsaNameSpaceInfoW;
  PWsaNameSpaceInfo = PWsaNameSpaceInfoW;
{$ELSE}
  WSANAMESPACE_INFO = WSANAMESPACE_INFOA;
  PWSANAMESPACE_INFO = PWSANAMESPACE_INFOA;
  LPWSANAMESPACE_INFO = LPWSANAMESPACE_INFOA;
  TWsaNameSpaceInfo = TWsaNameSpaceInfoA;
  PWsaNameSpaceInfo = PWsaNameSpaceInfoA;
{$ENDIF UNICODE}  

function accept(s: TSocket; addr: PSockAddr; addrlen: PINT): TSocket; stdcall;
function bind(s: TSocket; name: PSockAddr; namelen: Integer): Integer; stdcall;
function closesocket(s: TSocket): Integer; stdcall;
function connect(s: TSocket; name: PSockAddr; namelen: Integer): Integer; stdcall;
function ioctlsocket(s: TSocket; cmd: u_long; var argp: u_long): Integer; stdcall;
function getpeername(s: TSocket; name: PSockAddr; var namelen: Integer): Integer; stdcall;
function getsockname(s: TSocket; name: PSockAddr; var namelen: Integer): Integer; stdcall;
function getsockopt(s: TSocket; level, optname: Integer; optval: PAnsiChar; var optlen: Integer): Integer; stdcall;
function htonl(hostlong: u_long): u_long; stdcall;
function htons(hostshort: u_short): u_short; stdcall;
function inet_addr(cp: PAnsiChar): u_long; stdcall;
function inet_ntoa(inaddr: in_addr): PAnsiChar; stdcall;
function listen(s: TSocket; backlog: Integer): Integer; stdcall;
function ntohl(netlong: u_long): u_long; stdcall;
function ntohs(netshort: u_short): u_short; stdcall;
function recv(s: TSocket; var buf; len, flags: Integer): Integer; stdcall;
function recvfrom(s: TSocket; var buf; len, flags: Integer; from: PSockAddr; var fromlen: Integer): Integer; stdcall;
function select(nfds: Integer; readfds, writefds, exceptfds: PFdSet; timeout: PTimeVal): Integer; stdcall;
function send(s: TSocket; const buf; len, flags: Integer): Integer; stdcall;
function sendto(s: TSocket; const buf; len, flags: Integer; toaddr: PSockAddr; tolen: Integer): Integer; stdcall;
function setsockopt(s: TSocket; level, optname: u_long; optval: PAnsiChar; optlen: Integer): Integer; stdcall;
function shutdown(s: TSocket; how: Integer): Integer; stdcall;
function socket(af, type_, protocol: Integer): TSocket; stdcall;
function gethostbyaddr(addr: PAnsiChar; len, type_: Integer): PHostEnt; stdcall;
function gethostbyname(name: PAnsiChar): PHostEnt; stdcall;
function gethostname(name: PAnsiChar; namelen: Integer): Integer; stdcall;
function getservbyport(port: Integer; proto: PAnsiChar): PServEnt; stdcall;
function getservbyname(name, proto: PAnsiChar): PServEnt; stdcall;
function getprotobynumber(number: Integer): PProtoEnt; stdcall;
function getprotobyname(name: PAnsiChar): PProtoEnt; stdcall;
function WSAStartup(wVersionRequired: WORD; var lpWSAData: TWSAData): Integer; stdcall;
function WSACleanup: Integer; stdcall;
procedure WSASetLastError(iError: Integer); stdcall;
function WSAGetLastError: Integer; stdcall;
function WSAIsBlocking: BOOL; stdcall;
function WSAUnhookBlockingHook: Integer; stdcall;
function WSASetBlockingHook(lpBlockFunc: FARPROC): FARPROC; stdcall;
function WSACancelBlockingCall: Integer; stdcall;
function WSAAsyncGetServByName(hWnd: HWND; wMsg: u_int; name, proto, buf: PAnsiChar;
  buflen: Integer): THANDLE; stdcall;
function WSAAsyncGetServByPort(hWnd: HWND; wMsg: u_int; port: Integer;
  proto, buf: PAnsiChar; buflen: Integer): THandle; stdcall;
function WSAAsyncGetProtoByName(hWnd: HWND; wMsg: u_int; name, buf: PAnsiChar;
  buflen: Integer): THandle; stdcall;
function WSAAsyncGetProtoByNumber(hWnd: HWND; wMsg: u_int; number: Integer;
  buf: PAnsiChar; buflen: Integer): THandle; stdcall;
function WSAAsyncGetHostByName(hWnd: HWND; wMsg: u_int; name, buf: PAnsiChar;
  buflen: Integer): THandle; stdcall;
function WSAAsyncGetHostByAddr(hWnd: HWND; wMsg: u_int; addr: PAnsiChar;
  len, type_: Integer; buf: PAnsiChar; buflen: Integer): THandle; stdcall;
function WSACancelAsyncRequest(hAsyncTaskHandle: THandle): Integer; stdcall;
function WSAAsyncSelect(s: TSocket; hWnd: HWND; wMsg: u_int; lEvent: Longint): Integer; stdcall;
function WSAAccept(s: TSocket; addr: PSockAddr; addrlen: PINT;
  lpfnCondition: LPCONDITIONPROC; dwCallbackData: DWORD): TSocket; stdcall;
function WSACloseEvent(hEvent: WSAEVENT): BOOL; stdcall;
function WSAConnect(s: TSocket; name: PSockAddr; namelen: Integer; lpCallerData:
  LPWSABUF; lpCalleeData: LPWSABUF; lpSQOS: LPQOS; lpGQOS: LPQOS): Integer; stdcall;
function WSACreateEvent: WSAEVENT; stdcall;
function WSADuplicateSocketA(s: TSocket; dwProcessId: DWORD; lpProtocolInfo:
  LPWSAPROTOCOL_INFOA): Integer; stdcall;
function WSADuplicateSocketW(s: TSocket; dwProcessId: DWORD; lpProtocolInfo:
  LPWSAPROTOCOL_INFOW): Integer; stdcall;
function WSADuplicateSocket(s: TSocket; dwProcessId: DWORD; lpProtocolInfo:
  LPWSAPROTOCOL_INFO): Integer; stdcall;
function WSAEnumNetworkEvents(s: TSocket; hEventObject: WSAEVENT;
  lpNetworkEvents: LPWSANETWORKEVENTS): Integer; stdcall;
function WSAEnumProtocolsA(lpiProtocols: PINT; lpProtocolBuffer:
  LPWSAPROTOCOL_INFOA; var lpdwBufferLength: DWORD): Integer; stdcall;
function WSAEnumProtocolsW(lpiProtocols: PINT; lpProtocolBuffer:
  LPWSAPROTOCOL_INFOW; var lpdwBufferLength: DWORD): Integer; stdcall;
function WSAEnumProtocols(lpiProtocols: PINT; lpProtocolBuffer:
  LPWSAPROTOCOL_INFO; var lpdwBufferLength: DWORD): Integer; stdcall;
function WSAEventSelect(s: TSocket; hEventObject: WSAEVENT; lNetworkEvents: Longint): Integer; stdcall;
function WSAGetOverlappedResult(s: TSocket; lpOverlapped: LPWSAOVERLAPPED;
  var lpcbTransfer: DWORD; fWait: BOOL; var lpdwFlags: DWORD): BOOL; stdcall;
function WSAGetQOSByName(s: TSocket; var lpQOSName: WSABUF; lpQOS: LPQOS): BOOL; stdcall;
function WSAHtonl(s: TSocket; hostlong: u_long; var lpnetlong: u_long): Integer; stdcall;
function WSAHtons(s: TSocket; hostshort: u_short; var lpnetshort: u_short): Integer; stdcall;
function WSAIoctl(s: TSocket; dwIoControlCode: DWORD; lpvInBuffer: Pointer; cbInBuffer: DWORD; 
  lpvOutBuffer: Pointer; cbOutBuffer: DWORD; lpcbBytesReturned: PDWORD;
  lpOverlapped: LPWSAOVERLAPPED; lpCompletionRoutine:
    LPWSAOVERLAPPED_COMPLETION_ROUTINE): Integer; stdcall;
function WSAJoinLeaf(s: TSocket; name: PSockAddr; namelen: Integer;
  lpCallerData: LPWSABUF; lpCalleeData: LPWSABUF; lpSQOS, lpGQOS: LPQOS;
  dwFlags: DWORD): TSocket; stdcall;
function WSANtohl(s: TSocket; netlong: u_long; var lphostlong: u_long): Integer; stdcall;
function WSANtohs(s: TSocket; netshort: u_short; var lphostshort: u_short): Integer; stdcall;
function WSARecv(s: TSocket; lpBuffers: LPWSABUF; dwBufferCount: DWORD;
  var lpNumberOfBytesRecvd, lpFlags: DWORD; lpOverlapped: LPWSAOVERLAPPED;
  lpCompletionRoutine: LPWSAOVERLAPPED_COMPLETION_ROUTINE): Integer; stdcall;
function WSARecvDisconnect(s: TSocket; lpInboundDisconnectData: LPWSABUF): Integer; stdcall;
function WSARecvFrom(s: TSocket; lpBuffers: LPWSABUF; dwBufferCount: DWORD;
  var lpNumberOfBytesRecvd, lpFlags: DWORD; lpFrom: PSockAddr; lpFromlen: PINT;
  lpOverlapped: LPWSAOVERLAPPED; lpCompletionRoutine:
    LPWSAOVERLAPPED_COMPLETION_ROUTINE): Integer; stdcall;
function WSAResetEvent(hEvent: WSAEVENT): BOOL; stdcall;
function WSASend(s: TSocket; lpBuffers: LPWSABUF; dwBufferCount: DWORD;
  var lpNumberOfBytesSent: DWORD; dwFlags: DWORD; lpOverlapped: LPWSAOVERLAPPED;
  lpCompletionRoutine: LPWSAOVERLAPPED_COMPLETION_ROUTINE): Integer; stdcall;
function WSASendDisconnect(s: TSocket; lpOutboundDisconnectData: LPWSABUF): Integer; stdcall;
function WSASendTo(s: TSocket; lpBuffers: LPWSABUF; dwBufferCount: DWORD;
  var lpNumberOfBytesSent: DWORD; dwFlags: DWORD; lpTo: PSockAddr; iTolen: Integer;
  lpOverlapped: LPWSAOVERLAPPED; lpCompletionRoutine:
    LPWSAOVERLAPPED_COMPLETION_ROUTINE): Integer; stdcall;
function WSASetEvent(hEvent: WSAEVENT): BOOL; stdcall;
function WSASocketA(af, type_, protocol: Integer; lpProtocolInfo:
  LPWSAPROTOCOL_INFOA; g: GROUP; dwFlags: DWORD): TSocket; stdcall;
function WSASocketW(af, type_, protocol: Integer; lpProtocolInfo:
  LPWSAPROTOCOL_INFOW; g: GROUP; dwFlags: DWORD): TSocket; stdcall;
function WSASocket(af, type_, protocol: Integer; lpProtocolInfo:
  LPWSAPROTOCOL_INFO; g: GROUP; dwFlags: DWORD): TSocket; stdcall;
function WSAWaitForMultipleEvents(cEvents: DWORD; lphEvents: PWSAEVENT;
  fWaitAll: BOOL; dwTimeout: DWORD; fAlertable: BOOL): DWORD; stdcall;
function WSAAddressToStringA(lpsaAddress: LPSOCKADDR; dwAddressLength: DWORD;
  lpProtocolInfo: LPWSAPROTOCOL_INFOA; lpszAddressString: LPSTR;
  var lpdwAddressStringLength: DWORD): Integer; stdcall;
function WSAAddressToStringW(lpsaAddress: LPSOCKADDR; dwAddressLength: DWORD;
  lpProtocolInfo: LPWSAPROTOCOL_INFOW; lpszAddressString: LPWSTR;
  var lpdwAddressStringLength: DWORD): Integer; stdcall;
function WSAAddressToString(lpsaAddress: LPSOCKADDR; dwAddressLength: DWORD;
  lpProtocolInfo: LPWSAPROTOCOL_INFO; lpszAddressString: LPTSTR;
  var lpdwAddressStringLength: DWORD): Integer; stdcall;
function WSAStringToAddressA(AddressString: LPSTR; AddressFamily: Integer;
  lpProtocolInfo: LPWSAPROTOCOL_INFOA; lpAddress: LPSOCKADDR;
  var lpAddressLength: Integer): Integer; stdcall;
function WSAStringToAddressW(AddressString: LPWSTR; AddressFamily: Integer;
  lpProtocolInfo: LPWSAPROTOCOL_INFOW; lpAddress: LPSOCKADDR;
  var lpAddressLength: Integer): Integer; stdcall;
function WSAStringToAddress(AddressString: LPTSTR; AddressFamily: Integer;
  lpProtocolInfo: LPWSAPROTOCOL_INFO; lpAddress: LPSOCKADDR;
  var lpAddressLength: Integer): Integer; stdcall;
function WSALookupServiceBeginA(lpqsRestrictions: LPWSAQUERYSETA;
  dwControlFlags: DWORD; var lphLookup: THandle): Integer; stdcall;
function WSALookupServiceBeginW(lpqsRestrictions: LPWSAQUERYSETW;
  dwControlFlags: DWORD; var lphLookup: THandle): Integer; stdcall;
function WSALookupServiceBegin(lpqsRestrictions: LPWSAQUERYSET;
  dwControlFlags: DWORD; var lphLookup: THandle): Integer; stdcall;
function WSALookupServiceNextA(hLookup: THandle; dwControlFlags: DWORD;
  var lpdwBufferLength: DWORD; lpqsResults: LPWSAQUERYSETA): Integer; stdcall;
function WSALookupServiceNextW(hLookup: THandle; dwControlFlags: DWORD;
  var lpdwBufferLength: DWORD; lpqsResults: LPWSAQUERYSETW): Integer; stdcall;
function WSALookupServiceNext(hLookup: THandle; dwControlFlags: DWORD;
  var lpdwBufferLength: DWORD; lpqsResults: LPWSAQUERYSET): Integer; stdcall;
function WSANSPIoctl(hLookup: THandle; dwControlCode: DWORD; lpvInBuffer:
  Pointer; cbInBuffer: DWORD; lpvOutBuffer: Pointer; cbOutBuffer: DWORD;
  lpcbBytesReturned: LPDWORD; lpCompletion: LPWSACOMPLETION): Integer; stdcall;
function WSALookupServiceEnd(hLookup: THandle): Integer; stdcall;
function WSAInstallServiceClassA(lpServiceClassInfo: LPWSASERVICECLASSINFOA): Integer; stdcall;
function WSAInstallServiceClassW(lpServiceClassInfo: LPWSASERVICECLASSINFOW): Integer; stdcall;
function WSAInstallServiceClass(lpServiceClassInfo: LPWSASERVICECLASSINFO): Integer; stdcall;
function WSARemoveServiceClass(const lpServiceClassId: TGUID): Integer; stdcall;
function WSAGetServiceClassInfoA(const lpProviderId, lpServiceClassId: TGUID;
  var lpdwBufSize: DWORD; lpServiceClassInfo: LPWSASERVICECLASSINFOA): Integer; stdcall;
function WSAGetServiceClassInfoW(const lpProviderId, lpServiceClassId: TGUID;
  var lpdwBufSize: DWORD; lpServiceClassInfo: LPWSASERVICECLASSINFOW): Integer; stdcall;
function WSAGetServiceClassInfo(const lpProviderId, lpServiceClassId: TGUID;
  var lpdwBufSize: DWORD; lpServiceClassInfo: LPWSASERVICECLASSINFO): Integer; stdcall;
function WSAEnumNameSpaceProvidersA(var lpdwBufferLength: DWORD; lpnspBuffer:
  LPWSANAMESPACE_INFOA): Integer; stdcall;
function WSAEnumNameSpaceProvidersW(var lpdwBufferLength: DWORD; lpnspBuffer:
  LPWSANAMESPACE_INFOW): Integer; stdcall;
function WSAEnumNameSpaceProviders(var lpdwBufferLength: DWORD; lpnspBuffer:
  LPWSANAMESPACE_INFO): Integer; stdcall;
function WSAGetServiceClassNameByClassIdA(const lpServiceClassId: TGUID;
  lpszServiceClassName: LPSTR; var lpdwBufferLength: DWORD): Integer; stdcall;
function WSAGetServiceClassNameByClassIdW(const lpServiceClassId: TGUID;
  lpszServiceClassName: LPWSTR; var lpdwBufferLength: DWORD): Integer; stdcall;
function WSAGetServiceClassNameByClassId(const lpServiceClassId: TGUID;
  lpszServiceClassName: LPTSTR; var lpdwBufferLength: DWORD): Integer; stdcall;
function WSASetServiceA(lpqsRegInfo: LPWSAQUERYSETA; essoperation:
  WSAESETSERVICEOP;
  dwControlFlags: DWORD): Integer; stdcall;
function WSASetServiceW(lpqsRegInfo: LPWSAQUERYSETW; essoperation:
  WSAESETSERVICEOP;
  dwControlFlags: DWORD): Integer; stdcall;
function WSASetService(lpqsRegInfo: LPWSAQUERYSET; essoperation:
  WSAESETSERVICEOP;
  dwControlFlags: DWORD): Integer; stdcall;
function WSAProviderConfigChange(var lpNotificationHandle: THandle;
  lpOverlapped: LPWSAOVERLAPPED; lpCompletionRoutine:
    LPWSAOVERLAPPED_COMPLETION_ROUTINE): Integer; stdcall;

type
  PSOCKADDR_IN = ^sockaddr_in;
  LPSOCKADDR_IN = ^sockaddr_in;
  LPLINGER = PLINGER;
  PIN_ADDR = ^in_addr;
  LPIN_ADDR = ^in_addr;
  PFD_SET = ^fd_set;
  LPFD_SET = ^fd_set;

  LPHOSTENT = PHOSTENT;
  LPSERVENT = PSERVENT;
  LPPROTOENT = PPROTOENT;
  LPTIMEVAL = PTIMEVAL;

function WSAMAKEASYNCREPLY(buflen, error: WORD): DWORD;
function WSAMAKESELECTREPLY(event, error: WORD): DWORD;
function WSAGETASYNCBUFLEN(lParam: DWORD): WORD;
function WSAGETASYNCERROR(lParam: DWORD): WORD;
function WSAGETSELECTEVENT(lParam: DWORD): WORD;
function WSAGETSELECTERROR(lParam: DWORD): WORD;

implementation

const
  ws2_32 = 'ws2_32.dll';
{$IFDEF UNICODE}
  AWSuffix = 'W';
{$ELSE}
  AWSuffix = 'A';
{$ENDIF UNICODE}

procedure FD_CLR(fd: TSocket; var fdset: TFdSet);
var
  I: Cardinal;
begin
  I := 0;
  while I < fdset.fd_count do
  begin
    if fdset.fd_array[I] = fd then
    begin
      while I < fdset.fd_count - 1 do
      begin
        fdset.fd_array[I] := fdset.fd_array[I + 1];
        Inc(I);
      end;
      fdset.fd_count := fdset.fd_count - 1;
      Break;
    end;
    Inc(I);
  end;
end;

procedure _FD_SET(fd: TSocket; var fdset: TFDSet);
var
  I: Cardinal;
begin
  I := 0;
  while I < fdset.fd_count do
  begin
    if fdset.fd_array[I] = fd then
      Break;
    Inc(I);
  end;
  if I = fdset.fd_count then
  begin
    if fdset.fd_count < FD_SETSIZE then
    begin
      fdset.fd_array[I] := fd;
      fdset.fd_count := fdset.fd_count + 1;
    end;
  end;
end;

procedure FD_ZERO(var fdset: TFdSet);
begin
  fdset.fd_count := 0;
end;

function FD_ISSET(fd: TSocket; var fdset: TFdSet): Boolean;
begin
  Result := __WSAFDIsSet(fd, fdset) <> 0;
end;

function timerisset(const tvp: TTimeVal): Boolean;
begin
  Result := (tvp.tv_sec <> 0) or (tvp.tv_usec <> 0);
end;

procedure timerclear(var tvp: TTimeVal);
begin
  tvp.tv_sec := 0;
  tvp.tv_usec := 0;
end;

function _IO(x, y: DWORD): DWORD;
begin
  Result := IOC_VOID or (x shl 8) or y;
end;

function _IOR(x, y, t: DWORD): DWORD;
begin
  Result := IOC_OUT or ((T and IOCPARM_MASK) shl 16) or (x shl 8) or y;
end;

function _IOW(x, y, t: DWORD): DWORD;
begin
  Result := DWORD(IOC_IN or ((T and IOCPARM_MASK) shl 16) or (x shl 8) or y);
end;

function IN_CLASSA(i: DWORD): Boolean;
begin
  Result := i and DWORD($80000000) = 0;
end;

function IN_CLASSB(i: DWORD): Boolean;
begin
  Result := i and DWORD($C0000000) = DWORD($80000000);
end;

function IN_CLASSC(i: DWORD): Boolean;
begin
  Result := (i and DWORD($E0000000)) = DWORD($C0000000);
end;

function IN_CLASSD(i: DWORD): Boolean;
begin
  Result := (i and DWORD($F0000000)) = DWORD($E0000000);
end;

function IN_MULTICAST(i: DWORD): Boolean;
begin
  Result := IN_CLASSD(i);
end;

function h_errno: Integer;
begin
  Result := WSAGetLastError;
end;

function _WSAIO(x, y: DWORD): DWORD;
begin
  Result := IOC_VOID or x or y;
end;

function _WSAIOR(x, y: DWORD): DWORD;
begin
  Result := IOC_OUT or x or y;
end;

function _WSAIOW(x, y: DWORD): DWORD;
begin
  Result := IOC_IN or x or y;
end;

function _WSAIORW(x, y: DWORD): DWORD;
begin
  Result := IOC_INOUT or x or y;
end;

function WSAMAKEASYNCREPLY(buflen, error: WORD): DWORD;
begin
  Result := MAKELONG(buflen, error);
end;

function WSAMAKESELECTREPLY(event, error: WORD): DWORD;
begin
  Result := MAKELONG(event, error);
end;

function WSAGETASYNCBUFLEN(lParam: DWORD): WORD;
begin
  Result := LOWORD(lParam);
end;

function WSAGETASYNCERROR(lParam: DWORD): WORD;
begin
  Result := HIWORD(lParam);
end;

function WSAGETSELECTEVENT(lParam: DWORD): WORD;
begin
  Result := LOWORD(lParam);
end;

function WSAGETSELECTERROR(lParam: DWORD): WORD;
begin
  Result := HIWORD(lParam);
end;

function __WSAFDIsSet; external ws2_32 name '__WSAFDIsSet';
function accept; external ws2_32 name 'accept';
function bind; external ws2_32 name 'bind';
function closesocket; external ws2_32 name 'closesocket';
function connect; external ws2_32 name 'connect';
function ioctlsocket; external ws2_32 name 'ioctlsocket';
function getpeername; external ws2_32 name 'getpeername';
function getsockname; external ws2_32 name 'getsockname';
function getsockopt; external ws2_32 name 'getsockopt';
function htonl; external ws2_32 name 'htonl';
function htons; external ws2_32 name 'htons';
function inet_addr; external ws2_32 name 'inet_addr';
function inet_ntoa; external ws2_32 name 'inet_ntoa';
function listen; external ws2_32 name 'listen';
function ntohl; external ws2_32 name 'ntohl';
function ntohs; external ws2_32 name 'ntohs';
function recv; external ws2_32 name 'recv';
function recvfrom; external ws2_32 name 'recvfrom';
function select; external ws2_32 name 'select';
function send; external ws2_32 name 'send';
function sendto; external ws2_32 name 'sendto';
function setsockopt; external ws2_32 name 'setsockopt';
function shutdown; external ws2_32 name 'shutdown';
function socket; external ws2_32 name 'socket';
function gethostbyaddr; external ws2_32 name 'gethostbyaddr';
function gethostbyname; external ws2_32 name 'gethostbyname';
function gethostname; external ws2_32 name 'gethostname';
function getservbyport; external ws2_32 name 'getservbyport';
function getservbyname; external ws2_32 name 'getservbyname';
function getprotobynumber; external ws2_32 name 'getprotobynumber';
function getprotobyname; external ws2_32 name 'getprotobyname';
function WSAStartup; external ws2_32 name 'WSAStartup';
function WSACleanup; external ws2_32 name 'WSACleanup';
procedure WSASetLastError; external ws2_32 name 'WSASetLastError';
function WSAGetLastError; external ws2_32 name 'WSAGetLastError';
function WSAIsBlocking; external ws2_32 name 'WSAIsBlocking';
function WSAUnhookBlockingHook; external ws2_32 name 'WSAUnhookBlockingHook';
function WSASetBlockingHook; external ws2_32 name 'WSASetBlockingHook';
function WSACancelBlockingCall; external ws2_32 name 'WSACancelBlockingCall';
function WSAAsyncGetServByName; external ws2_32 name 'WSAAsyncGetServByName';
function WSAAsyncGetServByPort; external ws2_32 name 'WSAAsyncGetServByPort';
function WSAAsyncGetProtoByName; external ws2_32 name 'WSAAsyncGetProtoByName';
function WSAAsyncGetProtoByNumber; external ws2_32 name
  'WSAAsyncGetProtoByNumber';
function WSAAsyncGetHostByName; external ws2_32 name 'WSAAsyncGetHostByName';
function WSAAsyncGetHostByAddr; external ws2_32 name 'WSAAsyncGetHostByAddr';
function WSACancelAsyncRequest; external ws2_32 name 'WSACancelAsyncRequest';
function WSAAsyncSelect; external ws2_32 name 'WSAAsyncSelect';
function WSAAccept; external ws2_32 name 'WSAAccept';
function WSACloseEvent; external ws2_32 name 'WSACloseEvent';
function WSAConnect; external ws2_32 name 'WSAConnect';
function WSACreateEvent; external ws2_32 name 'WSACreateEvent';
function WSADuplicateSocketA; external ws2_32 name 'WSADuplicateSocketA';
function WSADuplicateSocketW; external ws2_32 name 'WSADuplicateSocketW';
function WSADuplicateSocket; external ws2_32 name 'WSADuplicateSocket' +
  AWSuffix;
function WSAEnumNetworkEvents; external ws2_32 name 'WSAEnumNetworkEvents';
function WSAEnumProtocolsA; external ws2_32 name 'WSAEnumProtocolsA';
function WSAEnumProtocolsW; external ws2_32 name 'WSAEnumProtocolsW';
function WSAEnumProtocols; external ws2_32 name 'WSAEnumProtocols' + AWSuffix;
function WSAEventSelect; external ws2_32 name 'WSAEventSelect';
function WSAGetOverlappedResult; external ws2_32 name 'WSAGetOverlappedResult';
function WSAGetQOSByName; external ws2_32 name 'WSAGetQOSByName';
function WSAHtonl; external ws2_32 name 'WSAHtonl';
function WSAHtons; external ws2_32 name 'WSAHtons';
function WSAIoctl; external ws2_32 name 'WSAIoctl';
function WSAJoinLeaf; external ws2_32 name 'WSAJoinLeaf';
function WSANtohl; external ws2_32 name 'WSANtohl';
function WSANtohs; external ws2_32 name 'WSANtohs';
function WSARecv; external ws2_32 name 'WSARecv';
function WSARecvDisconnect; external ws2_32 name 'WSARecvDisconnect';
function WSARecvFrom; external ws2_32 name 'WSARecvFrom';
function WSAResetEvent; external ws2_32 name 'WSAResetEvent';
function WSASend; external ws2_32 name 'WSASend';
function WSASendDisconnect; external ws2_32 name 'WSASendDisconnect';
function WSASendTo; external ws2_32 name 'WSASendTo';
function WSASetEvent; external ws2_32 name 'WSASetEvent';
function WSASocketA; external ws2_32 name 'WSASocketA';
function WSASocketW; external ws2_32 name 'WSASocketW';
function WSASocket; external ws2_32 name 'WSASocket' + AWSuffix;
function WSAWaitForMultipleEvents; external ws2_32 name
  'WSAWaitForMultipleEvents';
function WSAAddressToStringA; external ws2_32 name 'WSAAddressToStringA';
function WSAAddressToStringW; external ws2_32 name 'WSAAddressToStringW';
function WSAAddressToString; external ws2_32 name 'WSAAddressToString' +
  AWSuffix;
function WSAStringToAddressA; external ws2_32 name 'WSAStringToAddressA';
function WSAStringToAddressW; external ws2_32 name 'WSAStringToAddressW';
function WSAStringToAddress; external ws2_32 name 'WSAStringToAddress' +
  AWSuffix;
function WSALookupServiceBeginA; external ws2_32 name 'WSALookupServiceBeginA';
function WSALookupServiceBeginW; external ws2_32 name 'WSALookupServiceBeginW';
function WSALookupServiceBegin; external ws2_32 name 'WSALookupServiceBegin' +
  AWSuffix;
function WSALookupServiceNextA; external ws2_32 name 'WSALookupServiceNextA';
function WSALookupServiceNextW; external ws2_32 name 'WSALookupServiceNextW';
function WSALookupServiceNext; external ws2_32 name 'WSALookupServiceNext' +
  AWSuffix;
function WSANSPIoctl; external ws2_32 name 'WSANSPIoctl';
function WSALookupServiceEnd; external ws2_32 name 'WSALookupServiceEnd';
function WSAInstallServiceClassA; external ws2_32 name
  'WSAInstallServiceClassA';
function WSAInstallServiceClassW; external ws2_32 name
  'WSAInstallServiceClassW';
function WSAInstallServiceClass; external ws2_32 name 'WSAInstallServiceClass' +
  AWSuffix;
function WSARemoveServiceClass; external ws2_32 name 'WSARemoveServiceClass';
function WSAGetServiceClassInfoA; external ws2_32 name
  'WSAGetServiceClassInfoA';
function WSAGetServiceClassInfoW; external ws2_32 name
  'WSAGetServiceClassInfoW';
function WSAGetServiceClassInfo; external ws2_32 name 'WSAGetServiceClassInfo' +
  AWSuffix;
function WSAEnumNameSpaceProvidersA; external ws2_32 name
  'WSAEnumNameSpaceProvidersA';
function WSAEnumNameSpaceProvidersW; external ws2_32 name
  'WSAEnumNameSpaceProvidersW';
function WSAEnumNameSpaceProviders; external ws2_32 name
  'WSAEnumNameSpaceProviders' + AWSuffix;
function WSAGetServiceClassNameByClassIdA; external ws2_32 name
  'WSAGetServiceClassNameByClassIdA';
function WSAGetServiceClassNameByClassIdW; external ws2_32 name
  'WSAGetServiceClassNameByClassIdW';
function WSAGetServiceClassNameByClassId; external ws2_32 name
  'WSAGetServiceClassNameByClassId' + AWSuffix;
function WSASetServiceA; external ws2_32 name 'WSASetServiceA';
function WSASetServiceW; external ws2_32 name 'WSASetServiceW';
function WSASetService; external ws2_32 name 'WSASetService' + AWSuffix;
function WSAProviderConfigChange; external ws2_32 name
  'WSAProviderConfigChange';
  
end.


